#!usr/bin/python
# coding=utf-8
from utils.SingletonPattern import singleton
from components.localsystem import localsystem
from components.rom import rom
from components.device import device
from managers.globalarea import AppConfig


@singleton
class resourcesservice(object):

    def __init__(self):
        # 初始化本地系统对象，已做本地处理
        self.my_localsystem = localsystem()
        # 出事化打包对象， 已做打包任务
        self.my_rom = rom()
        # 移动终端机型和序列号对应关系 sn-> model
        self.mapsnmodel = {}
        # 配置类
        self.config = AppConfig()
        # 获取连接到设备上的移动终端设备 sn->state
        dictofdevices = self.my_localsystem.get_connected_devices()
        if dictofdevices is not None:
            # 初始化 dict_info 的状态为IDLE
            for key in dictofdevices.keys():
                self.config.dict_info[key] = device(key, dictofdevices[key], self.my_localsystem,
                                                    self.config.state["idle"])

    def getDeviceBySn(self, sn):
        return self.config.dict_info[sn]

    def getLocalsystem(self):
        return self.my_localsystem

    def getRom(self, params, private_logger, mi_account_name):
        return self.my_rom.check_status(
            self.my_rom.request_package(params[1], params[2], params[3], private_logger, mi_account_name),
            private_logger)
        # return self.my_rom.ret()

    def resetDeviceWorkState(self, sn):
        self.config.dict_info[sn].ws = self.config.state["idle"]
        print self.config.dict_info[sn].ws
        return self.config.dict_info[sn].ws == self.config.state["idle"]

    def setDeviceWorkStateBusy(self, sn):
        self.config.dict_info[sn].ws = self.config.state["busy"]
        print self.config.dict_info[sn].ws
        return self.config.dict_info[sn].ws == self.config.state["busy"]

    def getIdleDevices(self):
        return [d for d in self.config.dict_info.keys() if "idle" == self.config.dict_info[d].ws]

    def setAllIdleDevicesBusy(self):
        [self.setDeviceWorkStateBusy(d) for d in self.config.dict_info.keys() if "idle" == self.config.dict_info[d].ws]

    def getDeviceModelBySn(self, sn):
        return self.config.dict_info[sn].get_device_model()

    def getDeviceProductNameBySn(self, sn):
        return self.config.dict_info[sn].get_device_product_name()

    def getDeviceRegionBySn(self, sn):
        return self.config.dict_info[sn].get_device_region()

    def getAllDevicesWorkState(self):
        # 获取连接到设备上的移动终端设备 sn->state
        dictofdevices = self.my_localsystem.get_connected_devices()
        if dictofdevices is not None:
            # 初始化 dict_info 的状态为IDLE
            for key in dictofdevices.keys():
                if key not in self.config.dict_info.keys():
                    self.config.dict_info[key] = device(key, dictofdevices[key], self.my_localsystem,
                                                        self.config.state["idle"])
        return [{"product": str(d.get_device_model()) + "_" + str(d.ws), "sn": str(d.sn)} for d in
                self.config.dict_info.values()]

    def getDevicesInFastbootMode(self):
        return self.my_localsystem.get_connected_devices_in_fastboot_mode()
